package com.baomidou.springwind.msg;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.stereotype.Service;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * desc: 邮件发送
 *
 * @author xin.luo
 * @version V1.0
 * @since 2017/5/12 15:08
 */

@Service
public class MailSender {

    private String mailHost;

    private String mailProtocal;

    private String mailUsername;

    private String mailPassword;
    private String mailPort;

    private boolean enable;

    public void sendMail(String mailTitle, String mailContent, String... mailUsers) throws Exception {
        if (!enable) {
            return;
        }
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.exmail.qq.com");
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        //端口
        prop.setProperty("mail.smtp.port",mailPort);
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //使用JavaMail发送邮件的5个步骤
        //1、创建session
        Session session = Session.getInstance(prop);
        //2、通过session得到transport对象
        Transport ts = session.getTransport();
        //3、使用邮箱的用户名和密码连上邮件服务器
        ts.connect("smtp.exmail.qq.com", "glc@glc360.com", "GLOBALglc12345");
        //4、创建邮件
        Message message = createSimpleMail(session, mailTitle, mailContent, mailUsers);
        //5、发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
    }

    /**
     * @param session
     * @return
     * @throws Exception
     * @Method: createSimpleMail
     * @Description: 创建一封只包含文本的邮件
     */
    public MimeMessage createSimpleMail(Session session, String mailTitle, String mailContent, String... mailUsers) throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress(mailUsername));
        //指明邮件的收件人
        for (String mailUser : mailUsers) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailUser));
        }
        //邮件的标题
        message.setSubject(mailTitle);
        //邮件的文本内容
        message.setContent(mailContent, "text/html;charset=UTF-8");
        //返回创建好的邮件对象
        return message;
    }

    public String getMailHost() {
        return mailHost;
    }

    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }

    public String getMailProtocal() {
        return mailProtocal;
    }

    public void setMailProtocal(String mailProtocal) {
        this.mailProtocal = mailProtocal;
    }

    public String getMailUsername() {
        return mailUsername;
    }

    public void setMailUsername(String mailUsername) {
        this.mailUsername = mailUsername;
    }

    public String getMailPassword() {
        return mailPassword;
    }

    public void setMailPassword(String mailPassword) {
        this.mailPassword = mailPassword;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getMailPort() {
        return mailPort;
    }

    public void setMailPort(String mailPort) {
        this.mailPort = mailPort;
    }
}